package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.WorkIssue;
import com.bjsxt.model.Result;
import com.bjsxt.model.WorkIssueVo;
import com.bjsxt.service.WorkIssueService;
import com.bjsxt.util.SpringSecurityUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * description:
 * 客服工单控制层
 * 客户工单的权限:
 * 查询： work_issue_query
 * 回复： work_issue_update
 *
 * @author ALIENWARE <br />
 * @date 2021/7/20
 */
@RestController
@RequestMapping("/workIssues")
@Api(tags = "客服工单控制器")
public class WorkIssueController {

    /**
     * 客服工单业务层接口
     */
    @Autowired
    private WorkIssueService workIssueService;

    /**
     * 分页查询客服工单
     *
     * @param page      分页对象<WorkIssue>
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param status    状态
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('work_issue_query')")
    @ApiOperation(value = "分页查询客服工单", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query", example = "1999-10-12"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query", example = "1999-10-13"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query", example = "1")
    })
    public Result<Page<WorkIssueVo>> findByPage(
            @ApiIgnore Page<WorkIssue> page,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            Integer status
    ) {
        // 查询时间, 我们讲最近新增的, 修改的数据优先展示, 排序 last_update_time
        page.addOrder(OrderItem.desc(WorkIssue.COL_LAST_UPDATE_TIME));
        // 调用业务层进行分页数据的查询
        Page<WorkIssueVo> workIssueVoPage = workIssueService.pageByCreateTimeAndStatus(page, startTime, endTime, status);
        // 返回响应数据
        return Result.success(workIssueVoPage);
    }

    /**
     * 分页查询客服工单
     *
     * @param page 分页对象<WorkIssue>
     * @return 统一响应对象
     */
    @GetMapping("/issueList")
    @ApiOperation(value = "分页查询客服工单", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20")
    })
    public Result<Page<WorkIssue>> findByPage(@ApiIgnore Page<WorkIssue> page) {
        // 获取当前的用户ID
        Long userId = SpringSecurityUtils.getCurrentUserId();
        // 调用业务层进行分页数据的查询
        Page<WorkIssue> workIssuePage = workIssueService.pageByUserId(page, userId);
        // 返回响应数据
        return Result.success(workIssuePage);
    }

    /**
     * 增加客服工单数据
     *
     * @param workIssue 客服工单对象
     * @return 统一响应对象
     */
    @PostMapping("/addWorkIssue")
    @ApiOperation(value = "增加客服工单数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workIssue", value = "WorkIssue 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@RequestBody @Validated WorkIssue workIssue) {
        // 设置添加的用户 id
        workIssue.setUserId(SpringSecurityUtils.getCurrentUserId());
        // 设置状态
        workIssue.setStatus(1);
        // 调用业务层方法进行数据的新增
        boolean flag = workIssueService.save(workIssue);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 根据客服工单 ID 获取单个客服工单对象
     *
     * @param id 客服工单 ID
     * @return 统一响应对象
     */
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('work_issue_query')")
    @ApiOperation(value = "查询客服工单", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "客服工单 ID", dataType = "Integer", required = true, paramType = "query", example = "1")
    )
    public Result<WorkIssue> getById(@NotNull Long id) {
        // 根据客服工单 ID 查询客服工单数据
        WorkIssue workIssue = workIssueService.getById(id);
        // 返回响应数据
        return Result.success(workIssue);
    }

    /**
     * 回复客服工单
     *
     * @param id     需要回复的客服工单 ID
     * @param answer 回答工单的文字
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('work_issue_update')")
    @ApiOperation(value = "回复客服工单", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需要回复的客服工单 ID", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "answer", value = "回答工单的文字", dataType = "String", paramType = "query", example = "您好")
    })
    public Result<Void> update(@NotNull Long id, String answer) {
        // 创建需要更新的客服工单数据对象
        WorkIssue workIssue = new WorkIssue();
        // 设置客服工单更的值
        workIssue.setId(id);
        workIssue.setAnswer(answer);
        workIssue.setAnswerUserId(SpringSecurityUtils.getCurrentUserId());
        // 调用业务层方法进行数据的保存
        boolean flag = workIssueService.updateById(workIssue);
        // 如果为 true 回复成功
        if (flag) return Result.success("回复成功");
        // 如果为 false 回复失败
        return Result.fail("回复失败");
    }
}
